package com.queen.restaurant.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.queen.restaurant.base.BaseMessage;
import com.queen.restaurant.domain.CommentCourse;
import com.queen.restaurant.domain.CommentRestaurant;
import com.queen.restaurant.service.CommentCourseService;
import com.queen.restaurant.service.CommentRestaurantService;

@Controller
public class CommentController {

	@Autowired
	private CommentRestaurantService commentRestaurantService;
	@Autowired
	private CommentCourseService commentCourseService;
	// 提交餐厅评论
	@RequestMapping(value = "/comment/commit_restaurant_comment", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitRestaurantComment(
			@RequestParam("commentRestaurantJson") String commentRestaurantJson) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		int result = BaseMessage.ERROR;
		try {
			CommentRestaurant commentRestaurant = mapper.readValue(commentRestaurantJson, CommentRestaurant.class);
			commentRestaurantService.insertCommentRestaurant(commentRestaurant);
			result = BaseMessage.SUCCESS;
		} catch (JsonParseException e) {
			result = BaseMessage.ERROR;
		} catch (JsonMappingException e) {
			result = BaseMessage.ERROR;
		} catch (IOException e) {
			result = BaseMessage.ERROR;
		}
		
		resultMap.put("result", result);
		return resultMap;
	}
	
	// 提交菜品评论
	@RequestMapping(value = "/comment/commit_course_comment", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitCourseComment(
			@RequestParam("commentCourseJson") String commentCourseJson) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		int result = BaseMessage.ERROR;
		try {
			CommentCourse commentCourse = mapper.readValue(commentCourseJson, CommentCourse.class);
			commentCourseService.insertCommentCourse(commentCourse);
			result = BaseMessage.SUCCESS;
		} catch (JsonParseException e) {
			result = BaseMessage.ERROR;
		} catch (JsonMappingException e) {
			result = BaseMessage.ERROR;
		} catch (IOException e) {
			result = BaseMessage.ERROR;
		}
		
		resultMap.put("result", result);
		return resultMap;
	}
	
}
